import "../../../../scss/activity/cut_price/base_cut_edit.scss";
import $ from "jquery";
import "../../../utils/weui";
import moment from "moment";
import autosize from "../../../utils/autosize";
import com from "../../../utils/common";
import Vue from "vue";
import EditSection from "../../components/section";
import FuncBanner from "../../components/funcbanner";
import EditHead from "../../components/editHeader";
import EditTime from "../../components/editTime";
import EditAddMsg from "../../components/editAddFooter";
import defDate from "./cut_data";
import autoplay from "../../components/autoplaymusic";

$(function () {
  let page = {
    _actid: com.getUrlParam("actid") ? com.getUrlParam("actid") : 0,
    _actData:  defDate.edit,
    _app:{},
    _init: function () {
      let self = this;
      com
        .ajax({
          url: "/activity/getActDetial",
          data: {Actid: self._actid},
          ok: function (data) {
            console.log(data["msg"]);
            let temp = data["msg"];
            console.log(temp)
            if (temp !== "fail" && typeof temp === "object") {//如果没有取到值，则使用默认值，用户创建新活动时
              let tempData = {};
              tempData.Actid = self._actid;
              tempData.Prizes = temp.Prizes ? JSON.parse(temp.Prizes) : temp.Prizes;
              tempData.Prizes.Priprice = (tempData.Prizes.Priprice / 100).toFixed(2);
              tempData.Prizes.LatPrice = (tempData.Prizes.LatPrice / 100).toFixed(2);
              tempData.Content = JSON.parse(temp.Content);
              tempData.Header = {};
              // 存储标题音乐,以及头图
              tempData.Header.Title = temp.Title;
              tempData.Header.BgMusic = temp.BgMusic;
              tempData.Header.BannerStr = temp.BannerStr;
              // 存储时间
              tempData.TimeSet = {};
              tempData.TimeSet.StartTime = moment(temp.StartTime).format("YYYY-MM-DD HH:mm");
              tempData.TimeSet.EndTime = moment(temp.EndTime).format("YYYY-MM-DD HH:mm");
              // 存储额外信息(报名,电话等)
              tempData.AddMsg = {};
              tempData.AddMsg.Other = JSON.parse(data["msg"]["Other"]);
              console.log(temp.Mobile);
              tempData.AddMsg.Mobile = temp.Mobile;

              // 存储分享信息
              tempData.ShareContent = temp.ShareContent;
              tempData.ShareImg = temp.ShareImg;



              // 判断是否有人报名
              com.ajax({
                url:"/act/checkHasUser",
                data:{
                  Actid:self._actid
                },
                ok:function (data) {
                  console.log(data)
                  if(data["res"]){
                    self._app.HasJoiners = true;
                    self._app.OldPrizeNums = tempData.Prizes.PrizesNum;
                  }else{
                    self._app.HasJoiners = false;
                  }
                }
              })
              self._actData.HasJoiners = false;
              $.extend(self._actData, tempData);
              self._loadTemp();
            }else{
              self._loadTemp();
            }
            autoplay();
          }
        })
    },
    _loadTemp: function () {
      let self = this;
      Vue.component("g-section", EditSection);
      Vue.component("func-banner",FuncBanner);
      Vue.component("edit-header",EditHead);
      Vue.component("select-time",EditTime);
      Vue.component("edit-addmsg",EditAddMsg);
      self._app = new Vue({
        el: "#app",
        data: self._actData, //数据将由ajaxload进来，与默认的数据进行合并
        methods: {
          checkbox: function (item) {
            item = !item;
          },
          //Listen Section change
          listenSectionChange: function (somedata) {
            console.log(this.Content.Section)
          },
          //保存&预览活动
          saveActDetail: function (sta) {
            console.log(this);
            let selfData = this.$data;
            let parse = {};
            parse.Actid = selfData.Actid;
            parse.Catgory = "cut_price/base_cut";
            parse.StartTime = $("#start_time").val();
            parse.EndTime = $("#end_time").val();
            parse.Title = selfData.Header.Title;
            parse.BgMusic = selfData.Header.BgMusic;
            parse.BannerStr = selfData.Header.BannerStr;

            parse.Mobile = selfData.AddMsg.Mobile;

            let prizes = selfData.Prizes;
            prizes.Priprice = (selfData.Prizes.Priprice * 100).toFixed(0);
            prizes.LatPrice = (selfData.Prizes.LatPrice * 100).toFixed(0);

            parse.PrizesStr = JSON.stringify(prizes);
            parse.OtherStr = JSON.stringify(selfData.AddMsg.Other);
            parse.SectionStr = JSON.stringify(selfData.Content);
            parse.ShareImg = selfData.ShareImg;
            parse.ShareContent = selfData.ShareContent;
            parse.Status = sta;
            if (moment(parse.EndTime).isBefore(parse.StartTime)) {
              com.alertDialog("开始时间应该小于结束时间");
              return
            }
            console.log(this.OldPrizeNums)
            console.log(selfData.Prizes.PrizesNum)
            if(this.OldPrizeNums > selfData.Prizes.PrizesNum){
              com.alertDialog("已有人报名,不可减少奖品份数");
              return
            }
            console.log(parse);
            com.ajax({
              url: "/activity/saveCutPrice",
              data: parse,
              method: "post",
              ok: function (data) {
                console.log(data);
                if (data["msg"] == "ok") {
                  let ActId = data["Actid"]
                  console.log(ActId);
                  com.savedb(ActId,true);
                  let url = "";
                  if(sta == 1){
                    url = "/dist/activity/cut_price/base_cut.html?actid=" + ActId + "&preview=1"
                  }else{
                    url = "/dist/activity/cut_price/base_cut.html?actid=" + ActId
                  }
                  let editUrl = "/dist/activity/cut_price/base_cut_edit.html?actid=" + ActId
                  com.saveSuc(url,editUrl)
                  // 让用户可以在这个页面刷新一次
                  $(".layui-m-layershade").click(function () {
                    window.location = editUrl
                  })
                }
              }
            });
          }
        },
        filters: {
          moment(date) {
            let d = moment(date);
            if (!d.isValid(date)) return null;
            return d.format("YYYY-MM-DD HH:mm");
          }
        },
        updated: function () {
          // console.log("更新");
        }
      });
      Vue.nextTick(function () {
      });
      this._afterTemp();
    },
    _afterTemp: function () {
      let self = this;
      // 需要加载在模板渲染之后
      this.closeDailogEnable(); //注册事件
      autosize(document.querySelectorAll("textarea"));
    },
    //页面统一弹框关闭
    closeDailogEnable: function () {
      $("#app").on("click", ".cross ,.makesure", function () {
        $(this)
          .parent()
          .parent()
          .hide();
        $(".mask").hide();
        $(".management").show();
      });
    }
  };
  page._init();
});
